
package com.trkj.service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.IYzrMedCheckDAO;
import com.trkj.dao.jpa.IYzrPurchOrderDAO;
import com.trkj.dao.jpa.IYzrPurchOrderDetDAO;
import com.trkj.pojos.YzrMedCheck;
import com.trkj.pojos.YzrPurchOrder;
import com.trkj.pojos.YzrPurchOrderDet;

@Service
@Transactional
public class YzrCheckMedService {

	@Autowired
	private IYzrMedCheckDAO yzrMedCheckDAO;
	@Autowired
	private IYzrPurchOrderDAO yzrPurchOrderDAO;
	@Autowired
	private IYzrPurchOrderDetDAO yzrPurchOrderDetDAO;
	
	/**
	 * 提交验收数据
	 * @param list
	 * @param orderId
	 * @param staffId
	 * @param checkDate
	 */
	public void submitCheckMed(String[] list,String orderId,String staffId,Date checkDate){
		// 获取年份
		String year = getNowDate();
		//拼接
		String name = "YS-" + year;	
		String n = yzrMedCheckDAO.findId(name);
		System.out.println("从数据库中读取的最大采购编号："+n);

		//生成id
		YzrEntityService entityService = new YzrEntityService();
		String checkId = entityService.getId(name, n);
		
		//保存验收数据
		YzrMedCheck check = new YzrMedCheck();
		BigDecimal staffId2 = new BigDecimal(staffId);
		check.setMedCheckId(checkId);
		check.setMedCheckPerson(staffId2);
		check.setMedCheckPurchOrderId(orderId);
		check.setMedCheckTime(checkDate);
		yzrMedCheckDAO.save(check);
		
		//修改订单验收状态
		YzrPurchOrder order = yzrPurchOrderDAO.findOne(orderId);
		order.setPurchYanshouState("已验收");
		yzrPurchOrderDAO.save(order);
		
		//根据传过来的明细id判断药品是否合格，如果不合格修改该药品的数量为0
		List<YzrPurchOrderDet> listOrderDet = yzrPurchOrderDetDAO.findOrderDetByOrderId2(orderId);
		if(listOrderDet.size() == list.length){
			System.out.println("全部通过！");
		}else{
			int yy=listOrderDet.size();
			String [] ypo = new String[yy] ;
			for(int i=0;i<listOrderDet.size();i++){
				for(YzrPurchOrderDet yp:listOrderDet){
					ypo[i]=yp.getPurchOrderDetId();				
					//System.err.println("复制Det"+ypo[i]);
					i++;
				}
			}
			
			
			String[] jj=arrContrast(ypo,list);
			System.err.println("合并之后的大小："+jj.length);
			
			for(String j:jj){
				System.err.println("没有通过的编号:"+j);
				YzrPurchOrderDet orderDet = yzrPurchOrderDetDAO.findOne(j);
				orderDet.setOrderDetNumber(new BigDecimal(0));//数量设置为0
				orderDet.setOrderDetMoney(new BigDecimal(0));//金额设置为0
				yzrPurchOrderDetDAO.save(orderDet);
			}
		}
		
	}

	//取出不合格药品的id
	private static String[] arrContrast(String[] arr1, String[] arr2) {
		List<String> list = new LinkedList<String>();
		for (String str : arr1) { // 复制第一个数组到集合中
				list.add(str);
		}
		for (String str : arr2) { // 如果第二个数组存在和第一个数组相同的值，就删除
			if (list.contains(str)) {
				list.remove(str);
				System.err.println("移除的编号："+str);
			}
		}
		int chang = arr1.length - arr2.length;
		String[] result = new String[chang]; // 创建空数组
		int i=0;
		for(String temp: list){
			result[i]=temp;
			i++;
		}
		return list.toArray(result);
	}
	
	/**
	 * 格式化日期
	 * @return
	 */
	public static String getNowDate() {
		  Date currentTime = new Date();
		  SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		  String dateString = formatter.format(currentTime);
		  return dateString;
	}
}
